package com.example.yydm666.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yydm666.entity.User;
import com.example.yydm666.service.CartService;
import com.example.yydm666.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-03-28
 */
@Controller
@RequestMapping("/user")
@Api(tags = "用户登录模块")
public class UserController {
    @Autowired
    private UserService userService;

    private CartService cartService;

    /*
     * 注册
     * */
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public String register(User user, Model model) {
        boolean result = false;
//        判断账号唯一
        try {
            result = userService.save(user);
        } catch (Exception e) {
            model.addAttribute("error", user.getLoginName() + "已存在,请重新输入!");
            return "register";
        }
//        如果添加成功返回登录页面否则返回注册页面
        if (result) {
            return "login";
        }
        return "register";
    }


    /**
     * 登录验证
     */

    @ApiResponses({
            @ApiResponse(code = 200, message = "响应成功", response = String.class)
    })
    @ApiOperation(value = "登录验证")
    @RequestMapping("/login")
    public String login(@ApiParam(value = "用户名", required = true) String loginName,
                        @ApiParam(value = "用户密码", required = true) String password, Model model, HttpSession session) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("login_name", loginName);
        queryWrapper.eq("password", password);
        User user = userService.getOne(queryWrapper);
        if (user != null) {
            session.setAttribute("user", user);
            return "redirect:/productCategory/main";
        } else {
            model.addAttribute("error_login", "账号或密码不对请重新登录!");
            return "login";
        }
    }

    /**
     * 注销登录
     */
    @ApiOperation(value = "注销登录")
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
//        销毁session
        session.invalidate();
        return "redirect:/productCategory/main";
    }

    /*
     * 用户信息
     * */
    @ApiOperation(value = "用户信息")
    @RequestMapping("/userInfo")
    public ModelAndView userInfo(HttpSession session) {
        User user = (User) session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userInfo");
//        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }

    @RequestMapping("/userAddressList")
    public ModelAndView userAddressList() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userAddressList");
        return modelAndView;
    }


    //    后台登录
    @PostMapping("/deng")
    @ResponseBody
    public String deng(@RequestParam("loginName") String loginName, @RequestParam("password") String password, HttpSession session) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName, loginName);
        wrapper.eq(User::getPassword, password);
        User user = userService.getOne(wrapper);
        if (user != null){
            session.setAttribute("houuser", user);
            return "登录成功";
        } else {
            return "登录失败";
        }
    }


}

